<template>
  <PageWrapper title="新增或编辑活动" contentBackground contentClass="p-4" @back="goBack">
    <BasicForm
      @register="registerForm"
      :labelWidth="100"
      :schemas="schemas"
      :actionColOptions="{ span: 24 }"
      :showResetButton="false"
      @submit="handleSubmit"
      class="edit"
    />
  </PageWrapper>
</template>
<script lang="ts">
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { defineComponent, ref } from 'vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { PageWrapper } from '/@/components/Page';
  import { useGo } from '/@/hooks/web/usePage';
  import {
    createActivity,
    getActivityDetail,
    getActivityNameList,
    getEditActivityDetail,
  } from '/@/api/sys/activityList';
  import { useRouter } from 'vue-router';

  let infoName = ref('');
  let infoType = ref([]);
  let infoHdTimes = ref([]);
  let infoBeginTime = ref('');
  let infoEndTime = ref('');
  let infoMoney = ref([]);
  let infoHdid = ref([]);
  let type = ref<boolean>(false);
  let textSysNo = ref<number>(0);

  const schemas: FormSchema[] = [
    {
      field: 'name',
      component: 'Input',
      defaultValue: infoName,
      label: '活动名称',
      componentProps: {
        placeholder: '请输入活动名称',
      },
      required: true,
    },
    {
      field: 'type',
      defaultValue: infoType,
      component: 'ApiSelect',
      label: '活动周期',
      componentProps: {
        placeholder: '请选择周期',
        api: getActivityNameList,
        resultField: 'list',
        labelField: 'text',
        valueField: 'value',
      },
      required: true,
    },
    {
      field: 'time',
      component: 'RangePicker',
      defaultValue: [infoBeginTime, infoEndTime],
      label: '起止日期',
      required: true,
    },
    {
      field: 'hdTimes',
      defaultValue: infoHdTimes,
      component: 'Input',
      label: '可领取次数',
      componentProps: {
        placeholder: '请输入数字(限数字)',
        oninput: "value=value.replace(/[^0-9.]/g,'')",
      },
      required: true,
    },
    {
      field: 'money',
      defaultValue: infoMoney,
      component: 'Input',
      label: '付款金额(元)',
      componentProps: {
        placeholder: '请输入数字(限数字)',
        oninput: "value=value.replace(/[^0-9.]/g,'')",
      },
      required: true,
    },
  ];
  const [registerForm, {}] = useForm({
    fieldMapToTime: [['time', ['beginTime', 'endTime'], 'YYYY-MM-DD']],
    submitButtonOptions: { text: '提交' },
  });
  export default defineComponent({
    name: 'DocumentEdit',
    components: { BasicForm, PageWrapper },
    setup() {
      const go = useGo();
      const { currentRoute } = useRouter();
      const { createMessage } = useMessage();

      function goBack() {
        go('/document/activity');
      }

      (async () => {
        const { hd_id } = currentRoute.value.query;
        if (hd_id) {
          const res = await getActivityDetail({ hd_id: parseInt(hd_id as string) });
          console.log(res);
          infoName.value = res.hd_name;
          infoType.value = res.hd_type;
          infoHdTimes.value = res.hd_times;
          infoBeginTime.value = res.begin_time;
          infoEndTime.value = res.end_time;
          infoMoney.value = res.money;
          infoHdid.value = res.hd_id;
          textSysNo.value = res.hd_id;
          type.value = true;
        } else {
          type.value = false;
          infoName.value = '';
          infoType.value = [];
          infoHdTimes.value = [];
          infoBeginTime.value = '';
          infoEndTime.value = '';
          infoMoney.value = [];
          infoHdid.value = [];
          textSysNo.value = 0;
        }
      })();

      return {
        registerForm,
        schemas,
        infoName,
        infoType,
        infoHdTimes,
        infoBeginTime,
        infoEndTime,
        infoMoney,
        infoHdid,
        type,
        textSysNo,
        handleSubmit: async (values: any) => {
          if (type.value) {
            await getEditActivityDetail({
              name: values.name,
              type: values.type,
              hdTimes: values.hdTimes,
              beginTime: values.beginTime,
              endTime: values.endTime,
              money: values.money,
              hdid: textSysNo.value,
            });
            console.log('hdid');
            console.log(values.value);
            createMessage.success('编辑成功');
            go('/document/activity');
          } else {
            await createActivity({
              name: values.name,
              type: values.type,
              hdTimes: values.hdTimes,
              beginTime: values.beginTime,
              endTime: values.endTime,
              money: values.money,
            });
            console.log('createActivityhdid');
            console.log(values.value);
            createMessage.success('提交成功');
            go('/document/activity');
          }
        },
        goBack,
      };
    },
  });
</script>
